import java.net.*;
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;



public class hw2Client {

  public static String getDateTime() {
	  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	  Date date = new Date();
    return dateFormat.format(date);
  }
	
  public static void main(String[] args) {

    Socket theSocket;
    String hostname;
    String clientNum;
    String destNum;
    String message;
    BufferedReader theWtfBufferedReader;

   if (args.length == 0   ) {
        System.out.println("Usage: progName wtfServerPort wtfServerHost");
        System.exit(1);
    }

    if (args.length == 3) {
      hostname = args[1];
    }
    else {
      hostname = "localhost";
    }

    clientNum = args[2];
    destNum = args[3];
    message = args[4];
    
    try {
      theSocket = new Socket(hostname,Integer.parseInt(args[0]) );
	
      BufferedOutputStream bos = new BufferedOutputStream(theSocket.getOutputStream());
      
      OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
      osw.write(hw2Client.getDateTime() + clientNum + destNum + message);
      osw.flush();

      theWtfBufferedReader = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));

      String theWtfInputMessage = theWtfBufferedReader.readLine();
      System.out.println(theWtfInputMessage);

      
      theSocket.close();
    }  
    catch (UnknownHostException e) {
      System.err.println(e);
    }
    catch (IOException e) {
      System.err.println(e);
    }

  }  // end main

}
